package component;

import java.lang.reflect.Field;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import common.JCommonUtils;

import dominain.Contract;
import dominain.SystemDTO;
import error.JException;

public class GetDataComponetImpl implements GetDataComponet {

	@Override
	public SystemDTO getSystemDTO(Context context) throws JException {
		Field[] fields = SystemDTO.class.getDeclaredFields();

		final SharedPreferences sharedPreferences = context.getSharedPreferences(Contract.systemFile, Context.MODE_WORLD_READABLE);
		SystemDTO systemDTO = new SystemDTO();

		try {

			for (Field f : fields) {
				final String name = f.getName();
				Log.v("GetDataComponetImpl ", "fieldNmae=" + name);
				Object defValue = JCommonUtils.getObjValueByType(systemDTO, name);
				Object catchValue = null;
				if (Integer.class == f.getType() || int.class == f.getType()) {
					catchValue = sharedPreferences.getInt(name, (Integer) defValue);
				} else if (String.class == f.getType()) {
					catchValue = sharedPreferences.getString(name, (String) defValue);
				} else if (Boolean.class == f.getType() || boolean.class == f.getType()) {
					catchValue = sharedPreferences.getBoolean(name, (Boolean) defValue);
				}
				JCommonUtils.setObValueByType(systemDTO, name, catchValue);

			}
		} catch (Exception e) {
			throw new JException("GetDataComponetImpl", e.getMessage());
		}
		return systemDTO;
	}

}
